package com.caseProcess.controller;

import com.alibaba.fastjson.JSONObject;
import com.caseProcess.entity.CaseProcInfo;
import com.caseProcess.entity.Result;
import com.caseProcess.entity.Urging;
import com.caseProcess.entity.User;
import com.caseProcess.service.UrgingService;
import com.caseProcess.utils.IdWorker;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author duzq
 * @date 2020/11/15 15:45
 **/
@RestController
@RequestMapping("/urging")
public class UrgingController {

    @Resource
    public UrgingService urgingService;

    IdWorker idWorker = new IdWorker(1,1,1);


    /**
     * 添加催办信息
     * @param urgingJson 催办数据
     * @return 操作结果
     */
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/insert")
    public Result insert(String urgingJson) throws Exception {

        Urging urging = JSONObject.parseObject(urgingJson, Urging.class);
        urging.setUrgingId(idWorker.nextId()+"");

        Result result = new Result();
        int i = urgingService.insert(urging);
        if (i > 0){
            result.setCode(Result.RESULT_OK);
            result.setMsg("操作成功");
        }else{
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败");
        }
        return result;
    }

    /**
     * 根据催办id修改是否已读
     * @param urgingId
     * @param reviewContent
     * @return
     */
    @RequestMapping("/updateById")
    public Result updateById(String urgingId,String reviewContent){
        Result result = new Result();
        Urging urging = new Urging();
        urging.setUrgingId(urgingId);
        urging.setIsRead("1");
        urging.setReviewContent(reviewContent);
        int i = urgingService.update(urging);
        if (i > 0){
            result.setCode(Result.RESULT_OK);
            result.setMsg("催办信息修改成功");
        }else{
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("催办信息修改失败");
        }
        return result;
    }

    /**
     * 获取选中警情的催办信息
     * @param urgingId 催办id
     * @return
     */

    @RequestMapping("/getUrgingInfo")
    public Result gettUrgingInfo(String urgingId) {
        Result result = new Result();
        List<Urging> urgingList = urgingService.getUrgingInfo(urgingId);
        if (urgingList.size() >= 0) {
            result.setCode(Result.RESULT_OK);
            result.setMsg("催办信息查询成功");
            result.setCount(urgingList.size());
            result.setData(urgingList);
            System.out.println(urgingList);
            return result;

        } else {
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("服务器错误");
            return result;
        }

    }
}

